import requests

from utils.read_file import read_json


def send(method, path, payload=None):
    """3个参数method和path必传,payload可选"""
    if method in ['GET', 'POST', 'PUT', 'DELETE']:
        # 拼接接口地址url
        scheme = read_json('server.json', 'scheme')
        host = read_json('server.json', 'host')
        port = read_json('server.json', 'port')
        # 完整接口地址
        url = f'{scheme}://{host}:{port}' + path
        # headers
        headers = {
            "content-type": "application/json;charset=utf-8",
            "Authori-zation": f"Bearer {read_json('token.json', 'token')}"
        }

        if method == 'GET':
            return requests.get(url=url, headers=headers)

        elif method == 'POST':
            return requests.post(url=url, headers=headers, json=payload)

        elif method == 'PUT':
            return requests.put(url=url, headers=headers, json=payload)

        elif method == 'DELETE':
            return requests.delete(url=url, headers=headers)

    else:
        return '输入接口信息错误,无法执行接口'
